//
//  UpdateViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-30.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "UpdateViewController.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"
@interface UpdateViewController ()
{
    NSString *_newVersionId;
    NSString *_newVersionUrl;
    NSString *_versionInfo;
    NSString *_versionSize;
}
@end

@implementation UpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createMyNav];
    [self configView];
    [self checkVersionRequest];
}

- (void)createMyNav
{
    [self addTitle:@"版本更新" imageName:@"update"];
    [self addBackBtnTarget:self action:@selector(backBtnClick)];
}

- (void)configView
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 20+44*H, SCREEN_WIDTH, 150*H)];
    bgView.backgroundColor = [UIColor colorWithRed:247.f/255.f green:248.f/255.f blue:248.f/255.f alpha:1.0f];
    
    //logo
    UIImageView *versionImgView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-65*W)/2, 30*H, 65*H, 65*H)];
    [versionImgView setImage:[UIImage imageNamed:@"version_updating_icon"]];
    [bgView addSubview:versionImgView];
    
    //当前版本
    UILabel *versionLabel = [self createLabelFrame:CGRectMake(30*W, 30*H+CGRectGetHeight(versionImgView.frame)+10*H, SCREEN_WIDTH-60*W, 20*H) title:@"机车卫士v"CurrentVersion@"版" font:[UIFont systemFontOfSize:15]];
    versionLabel.textColor = [UIColor grayColor];
    [bgView addSubview:versionLabel];
    [self.view addSubview:bgView];
}

- (void)loadUpdateView
{
    //最新版本
    UILabel *newVersionLable = [self createLabelFrame:CGRectMake(30*W, 20+204*H, SCREEN_WIDTH-60*W, 20*H) title:[NSString stringWithFormat:@"最新版本：%@", _newVersionId] font:[UIFont systemFontOfSize:15]];
    newVersionLable.textColor = [UIColor grayColor];
    newVersionLable.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:newVersionLable];
    
    //新版本大小
    UILabel *newVersionSizeLabel = [self createLabelFrame:CGRectMake(30*W, CGRectGetMaxY(newVersionLable.frame), SCREEN_WIDTH-60*W, 20*H) title:[NSString stringWithFormat:@"新版本大小：%@", _versionSize] font:[UIFont systemFontOfSize:15]];
    newVersionSizeLabel.textColor = [UIColor grayColor];
    newVersionSizeLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:newVersionSizeLabel];
    
    //更新内容标题
    UILabel *updateLabel = [self createLabelFrame:CGRectMake(30*W, CGRectGetMaxY(newVersionSizeLabel.frame)+10*H, SCREEN_WIDTH-60*W, 20*H) title:@"更新内容" font:[UIFont systemFontOfSize:15]];
    updateLabel.textColor = [UIColor grayColor];
    updateLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:updateLabel];
    
    //更新内容
    //加了一个scrollView防止更新内容过多时“立即升级”按钮超出屏幕
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30*W, CGRectGetMaxY(updateLabel.frame), SCREEN_WIDTH-60*W, 160*H)];
    scrollView.bounces = NO;
    UILabel *contentLabel = [self createLabelFrame:CGRectMake(30*W, CGRectGetMaxY(updateLabel.frame), SCREEN_WIDTH-60*W, 60*H) title:_versionInfo font:[UIFont systemFontOfSize:15]];
    contentLabel.numberOfLines = 0;
    CGSize size = [contentLabel sizeThatFits:CGSizeMake(SCREEN_WIDTH-60*W, MAXFLOAT)];
    contentLabel.frame = CGRectMake(0, 0, size.width, size.height);
    contentLabel.textColor = [UIColor grayColor];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH-60*W, size.height);
    if (size.height < 160*H) {
        scrollView.frame = CGRectMake(30*W, CGRectGetMaxY(updateLabel.frame), SCREEN_WIDTH-60*W, size.height);
    }
    else {
        scrollView.frame = CGRectMake(30*W, CGRectGetMaxY(updateLabel.frame), SCREEN_WIDTH-60*W, 160*H);
    }
    [scrollView addSubview:contentLabel];
    [self.view addSubview:scrollView];
    
    UIButton *updateBtn = [self createBtnFrame:CGRectMake((SCREEN_WIDTH-112*W)/2, CGRectGetMaxY(scrollView.frame)+10*H, 112*W, 37*H) title:nil bgImageName:@"upgrade_now" selectedImageName:@"upgrade_now_on" target:self action:@selector(updateNow)];
    [self.view addSubview:updateBtn];
}

#pragma mark - 网络请求
//版本更新检查请求
- (void)checkVersionRequest
{
    PopLoadMsg *loadmsg = [PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    NSDictionary *paramdic = @{@"vesionid":CurrentVersion,@"type":@"1"};
    NSString *url = [urlstring stringByAppendingString:@"checkVersion.do"];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:url withParam:paramdic success:^(AFHTTPRequestOperation *op,id responseObject){
        [loadmsg delayImagesRemove];
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSLog(@"dic = %@", dic);
        
        NSInteger flag = [[dic objectForKey:@"flag"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        _newVersionId = [dic objectForKey:@"newVersionId"];
        _newVersionUrl = [dic objectForKey:@"newVersionUrl"];
        _versionInfo = [dic objectForKey:@"versionInfo"];
        _versionSize = [dic objectForKey:@"versionSize"];
        if (msg) {
            NSString *message=[msg stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [PopMsg showMessage:message Lasttime:1.0f];
        }
        
        if (flag == 0) {
            NSLog(@"当前是最新版本");
        }
        
        else if (flag == 1) {
            NSLog(@"有最新版本更新");
            [self loadUpdateView];
        }
        
    }failure:^(AFHTTPRequestOperation *op,NSError *error){
        [loadmsg delayImagesRemove];
        [PopMsg showMessage:@"网络异常，请重试" Lasttime:1.0f];
    }];
}

#pragma mark - 点击事件
- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)updateNow
{
    NSString *iTunesString = [[NSUserDefaults standardUserDefaults]objectForKey:APP_URL];
    if(iTunesString){
        NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
        [[UIApplication sharedApplication] openURL:iTunesURL];
    }
    else{
        [PopMsg showMessage:@"无法打开AppStore" Lasttime:1.0f];
    }
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
